package querylog.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import querylog.util.*;
import querylog.bean.*;

/**
 * @typeName LogDAOImpl
 * @Description: 交易日志DAO层实现
 * @author wangxy
 * @date 2017年11月30日下午2:24:15
 * @version v1.0
 */
public class LogDAOImpl implements LogDAO {
	
	/**
	 * @Title getLogHead
	 * @Description 根据流水号得到交易日志详细信息
	 * @param @param seq
	 * @param @return
	 * @param @throws SQLException
	 */
	@Override
	public LogHead getLogHead(String seq) throws SQLException {
		// TODO Auto-generated method stub
		LogHead lh = new LogHead();			//交易日志概要

		Connection conn = ManagerThreadLocal.getConnection();
		String sql = "select branch,hostid,indate,intime,reqinfo,respinfo,seqno,trancode,trandate,transtatus,trantime,userid,usetime from fm_log_head where seqno = ?";
		PreparedStatement ps = conn.prepareStatement(sql);
		ps.setString(1, seq);
		ResultSet rs = ps.executeQuery();
		while (rs.next()) {
			lh.setBranch(rs.getString("branch"));
			lh.setHostid(rs.getString("hostid"));
			lh.setIndate(rs.getString("indate"));
			lh.setIntime(rs.getString("intime"));
			lh.setReqinfo(rs.getString("reqinfo"));
			lh.setRespinfo(rs.getString("respinfo"));
			lh.setSeqno(rs.getString("seqno"));
			lh.setTrancode(rs.getString("trancode"));
			lh.setTrandate(rs.getString("trandate"));
			lh.setTranstatus(rs.getString("transtatus"));
			lh.setTrantime(rs.getString("trantime"));
			lh.setUserid(rs.getString("userid"));
			lh.setusetime(rs.getString("usetime"));

		}
		return lh;
	}
//目前作废
	/**
	 * @Title listLogBodys
	 * @Description 根据流水号查询指定的日志详细信息
	 * @param @param seq
	 * @param @return
	 * @param @throws SQLException
	 */
	@Override
	public List<LogBody> listLogBodys(String seq) throws SQLException {
		// TODO Auto-generated method stub
		List<LogBody> logbodylist = new ArrayList<LogBody>();
		LogBody lb = null;			//交易日志明细
		Connection conn = ManagerThreadLocal.getConnection();
		String sql = "select hostid,times,seqno,sessionid,loginfo from fm_log_body where seqno = ? order by times asc";
		PreparedStatement ps = conn.prepareStatement(sql);
		ps.setString(1, seq);
		ResultSet rs = ps.executeQuery();
		while (rs.next()) {
			lb = new LogBody();
			lb.setHostid(rs.getString("hostid"));
			lb.setTimes(rs.getString("times"));
			lb.setSeqno(rs.getString("seqno"));
			lb.setSessionid(rs.getString("sessionid"));
			lb.setLoginfo(rs.getString("loginfo"));
			logbodylist.add(lb);
		}
		return logbodylist;
	}


	
	/**
	 * @Title getLogHeads
	 * @Description 查询符合条件的日志头
	 * @param @param sql
	 * @param @return
	 * @param @throws SQLException
	 */
	@Override
	public List<LogHead> getLogHeads(String sql,int currentPage,int pageSize) throws SQLException {
		// TODO Auto-generated method stub
		List<LogHead> logheadlist = new ArrayList<LogHead>();
		LogHead ll = null;
		Connection conn = ManagerThreadLocal.getConnection();
		int startRecord = ((currentPage-1)*pageSize)+1;
		int endRecord = (currentPage*pageSize);
		
		String pageSql ="select * from (select a.*, rownum rn from ("+"select branch,hostid,indate,intime,reqinfo,respinfo,seqno,trancode,trandate,transtatus,trantime,userid,usetime from fm_log_head "+sql+") a where rownum<=?) where rn>=?";
		PreparedStatement ps = conn.prepareStatement(pageSql);
		ps.setInt(1, endRecord);
		ps.setInt(2, startRecord);
		//System.out.println(pageSql);
		//System.out.println("currentPage="+currentPage+"pageSize="+pageSize+"startRecord="+startRecord+"endRecord="+endRecord);
		
		ResultSet rs = ps.executeQuery();
		while (rs.next()) {
			ll = new LogHead();
			ll.setBranch(rs.getString("branch"));
			ll.setHostid(rs.getString("hostid"));
			ll.setIndate(rs.getString("indate"));
			ll.setIntime(rs.getString("intime"));
			ll.setReqinfo(rs.getString("reqinfo"));
			ll.setRespinfo(rs.getString("respinfo"));
			ll.setSeqno(rs.getString("seqno"));
			ll.setTrancode(rs.getString("trancode"));
			ll.setTrandate(rs.getString("trandate"));
			ll.setTranstatus(rs.getString("transtatus"));
			ll.setTrantime(rs.getString("trantime"));
			ll.setUserid(rs.getString("userid"));
			ll.setusetime(rs.getString("usetime"));
			logheadlist.add(ll);

		}
		return logheadlist;
	}

	/**
	 * @Title count
	 * @Description 得到符合条件的记录总数
	 * @param @param condition
	 * @param @return
	 * @param @throws SQLException
	 */
	@Override
	public int count(String condition) throws SQLException {
		// TODO Auto-generated method stub
		String sql="select count(*) from fm_log_head "+condition;
		//System.out.println(sql);
		Connection conn = ManagerThreadLocal.getConnection();
		PreparedStatement ps = conn.prepareStatement(sql);
		int recordNum = 0;
		ResultSet rs = ps.executeQuery();
		while (rs.next()) {
			recordNum = rs.getInt(1);
		}
		
		return recordNum;
	}

	/**
	 * @Title getLogbody
	 * @Description 得到交易日志明细
	 * @param @param seq
	 * @param @return
	 * @param @throws SQLException
	 */
	@Override
	public LogBody getLogbody(String seq) throws SQLException {
		LogBody lb = null;
		Connection conn = TomcatDBCPUtil.getConnection();
		String sql = "select hostid,times,seqno,sessionid,loginfo from fm_log_body where seqno = ?";
		PreparedStatement ps = conn.prepareStatement(sql);
		ps.setString(1, seq);
		ResultSet rs = ps.executeQuery();
		while (rs.next()) {
			lb = new LogBody();
			lb.setHostid(rs.getString("hostid"));
			lb.setSeqno(rs.getString("seqno"));
			lb.setSessionid(rs.getString("sessionid"));
			lb.setTimes(rs.getString("times"));
			lb.setLoginfo(rs.getString("loginfo"));
		}
		return lb;
	}

}
